package com.owen.springboot.smartparkingms.module.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.common.util.DateUtils;
import com.owen.springboot.smartparkingms.common.util.ShiroUtils;
import com.owen.springboot.smartparkingms.module.app.entity.CarParkRecord;
import com.owen.springboot.smartparkingms.module.app.entity.Park;
import com.owen.springboot.smartparkingms.module.app.mapper.CarParkRecordMapper;
import com.owen.springboot.smartparkingms.module.app.service.ICarParkRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.owen.springboot.smartparkingms.module.sys.entity.SysUser;
import com.owen.springboot.smartparkingms.module.sys.mapper.UserParkMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@Service
public class CarParkRecordServiceImpl extends ServiceImpl<CarParkRecordMapper, CarParkRecord> implements ICarParkRecordService {

    @Resource
    CarParkRecordMapper carParkRecordMapper;

    @Resource
    UserParkMapper userParkMapper;

    @Override
    public Result listPage(CarParkRecord record) {
        IPage<CarParkRecord> page = new Page(record.getPageNo(),record.getPageSize());
        SysUser sysUser = ShiroUtils.getUserEntity();
        List<Long> parkIdList = userParkMapper.selectparkId(sysUser.getUserId());
        String beginTime = record.getBeginTime();
        String endTime = record.getEndTime();

        System.out.println("Test!!!"+ record.getBeginTime());

        QueryWrapper query = new QueryWrapper();
        query.in("park_id",parkIdList);
        query.like("plate_number",record.getDescription());
        if(record.getParkId()!=null){
            query.eq("park_id",record.getParkId());
        }
        if (StringUtils.isNotBlank(beginTime)){
            beginTime = DateUtils.GMTformat(record.getBeginTime());
            query.ge("gmt_into", beginTime);
        }
        if (StringUtils.isNotBlank(endTime)){
            endTime = DateUtils.GMTformat(record.getEndTime());
            query.le("gmt_out", endTime);
        }
        query.orderByDesc("gmt_into");
        IPage<Park> list =carParkRecordMapper.selectPage(page,query);
        return Result.ok(list);
    }
}
